﻿#region [ Modification History ]
/*
 * 06/14/2013 Denis Kholine
 *  Generate source code
 */
#endregion

#region  [ UIUC NCSA Open Source License ]
/*****************************************************************************\
 *             University of Illinois/NCSA Open Source License
 *
 * 
 *      Copyright © 2012,2013, The Board of Trustees of the University of Illinois. All rights reserved.
 *
 *      Developed by: Rakesh Bobba, Derek Dagit, Erich Heine, Denis Kholine,
 *                    Klara Nahrstedt, David Rodgers,   William H. Sanders, Tim Yardley
 *
 *      Information Trust Institute/Coordinated Science Laboratory
 * 
 *      University of Illinois
 *      
 *      The Board of Trustees of the University of Illinois
 *      109 Coble Hall
 *      801 South Wright Street
 *      Champaign, IL 61820-6242
 *
 *      http://www.iti.illinois.edu / http://csl.illinois.edu
 * 
 *      Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
 *      associated documentation files (the “Software”), to deal with the Software without restriction, 
 *      including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *      and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 *      subject to the following conditions:
 *
 *      Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *      following disclaimers.
 *      
 *      Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
 *      the following disclaimers in the documentation and/or other materials provided with the distribution.
 *
 *      Neither the names of Information Trust institute, Coordinated Science Laboratory, University of Illinois,
 *      The Board of Trustees of the University of Illinois nor the names of its contributors may be used to 
 *      endorse or promote products derived from this Software without specific prior written permission.
 *      
 *      THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 *      LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN
 *      NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *      WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 *      SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 *      
 \*****************************************************************************/
#endregion

#region [ Using ]
using GSF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using SIEGate.Core;
using SIEGate.Core.Stats;
using SIEGate.Core.Fakes;
using SIEGate.Core.Notifications;
using SIEGate.Core.Measurements;
using SIEGate.Core.Logging;
using SIEGate.Core.Legacy;
using SIEGate.Core.Adapters;
using SIEGate.Core.MessageBusses;
using GSF.TimeSeries.Transport;
#endregion


namespace SIEGateCoreTest
{
    /// <summary>
    /// A ConfigurationTables class.
    /// NOTE: This class 
    /// </summary>
    class ConfigurationTables 
    {
        #region [ Members ]
 
 
        /// <summary>
        /// Action Adapter data table
        /// </summary>
        private DataTable dtActionAdapter;
        /// <summary>
        /// Action Adapter Active Measurments Active Measurements
        /// </summary>
        private DataTable dtActionAdapterActiveMeasurements;
        /// <summary>
        /// Action Adapter Statistic
        /// </summary>
        private DataTable dtActionAdapterStatistics;
        /// <summary>
        /// Input Adapter data table
        /// </summary>
        private DataTable dtInputAdapter;
        /// <summary>
        /// Active Measurements data table
        /// </summary>
        private DataTable dtInputAdapterActiveMeasurements;
        /// <summary>
        /// Input Adapter Statistics
        /// </summary>
        private DataTable dtInputAdapterStatistics;
        /// <summary>
        /// Container for test Measurements
        /// </summary>
        private DataTable dtMeasurement;
        /// <summary>
        /// Output Adapter data table
        /// </summary>
        private DataTable dtOutputAdapter;
        /// <summary>
        /// Output Adapter Active Measurements data table
        /// </summary>
        private DataTable dtOutputAdapterActiveMeasurements;
        /// <summary>
        /// Output Adapter Statistics
        /// </summary>
        private DataTable dtOutputAdapterStatistics;
        /// <summary>
        /// Statistics data table
        /// </summary>
        private DataTable dtStatistics;
        /// <summary>
        /// Action Adapter
        /// </summary>
        private ActionAdapter m_ActionAdapter;
        /// <summary>
        /// Measurement Case
        /// </summary>
        private Measurement m_Measurement;
        /// <summary>
        /// Input Adapter
        /// </summary>
        private InputAdapter m_InputAdapter;
        /// <summary>
        /// Output Adapter
        /// </summary>
        private OutputAdapter m_OutputAdapter;
        /// <summary>
        /// Input Adapter DataSet
        /// </summary>
        private DataSet dataSetInput;
        /// <summary>
        /// Action Adapter DataSet
        /// </summary>
        private DataSet dataSetAction;
        /// <summary>
        /// Output Adapter DataSet
        /// </summary>
        private DataSet dataSetOutput;
        #endregion

        #region [ Properties ]
        /// <summary>
        /// Input Adapter
        /// </summary>
        public IAdapter InputAdapter
        {
            get
            {
                return m_InputAdapter;
            }
        }
        /// <summary>
        /// Action Adapter
        /// </summary>
        public IAdapter ActionAdapter
        {
            get
            {
                return m_ActionAdapter;
            }
        }
        /// <summary>
        /// Output Adapter
        /// </summary>
        public IAdapter OutputAdapter
        {
            get
            {
                return m_OutputAdapter;
            }
        }
        /// <summary>
        /// Input Adapter data set
        /// </summary>
        public DataSet DataSetInputAdapter
        {
            get
            {
                return dataSetInput;
            }
        }
        /// <summary>
        /// Action Adapter data set
        /// </summary>
        public DataSet DataSetActionAdapter
        {
            get
            {
                return dataSetAction;
            }
        }
        /// <summary>
        /// Output Adapter data set
        /// </summary>
        public DataSet DataSetOutputAdapter
        {
            get
            {
                return dataSetOutput;
            }
        }
        #endregion

        #region [ Constructos ]
        public ConfigurationTables()
        {
            #region [ Initialization ]
            Guid streamID = Guid.NewGuid();
            Ticks timestamp = new Ticks();
            MeasurementStateFlags flags = MeasurementStateFlags.Normal;
            IMeasurementValue value;
            #endregion

            #region [ Incoming/Outgoing IDs ]
            ISet<Guid> incoming_val = new HashSet<Guid>();
            ISet<Guid> outgoing_val = new HashSet<Guid>();

            incoming_val.Add(Guid.NewGuid());
            outgoing_val.Add(Guid.NewGuid());
            #endregion

            #region [ Configuration ]
            IAdapterInfo conf_input;
            IAdapterInfo conf_action;
            IAdapterInfo conf_output;

            conf_input = new MockAdapter.MockAdapterInfo(incoming_val, outgoing_val);
            conf_action = new MockAdapter.MockAdapterInfo(incoming_val, outgoing_val);
            conf_output = new MockAdapter.MockAdapterInfo(incoming_val, outgoing_val);

            conf_input.ID = (uint)10;
            conf_action.ID = (uint)20;
            conf_output.ID = (uint)30;

            conf_input.Name = "InputAdapter";
            conf_action.Name = "ActionAdapter";
            conf_output.Name = "OutputAdapter"; 

            dataSetInput = new DataSet();
            dataSetAction = new DataSet();
            dataSetOutput = new DataSet();
            #endregion

            // Statistics applied for all adapters
            #region [ Statistics Data ]
            /**********************************************************************/
            // Build statistical information
            dtStatistics = new DataTable("Statistics");
            dtStatistics.Columns.Add("Enabled");
            dtStatistics.Columns.Add("Source");
            dtStatistics.Columns.Add("Name");
            dtStatistics.Columns.Add("SignalIndex");
            dtStatistics.Columns.Add("Arguments");
            dtStatistics.Columns.Add("AssemblyName");
            dtStatistics.Columns.Add("TypeName");
            dtStatistics.Columns.Add("MethodName");

            // Add data rows into statistics
            DataRow dtStatisticsRow = dtStatistics.NewRow();

            dtStatisticsRow["Enabled"] = ("1").ToString();
            dtStatisticsRow["Source"] = ("System").ToString();
            dtStatisticsRow["Name"] = ("CPU Usage").ToString();
            dtStatisticsRow["SignalIndex"] = ("1").ToString();
            dtStatisticsRow["Arguments"] = ("").ToString();
            dtStatisticsRow["AssemblyName"] = ("TimeSeriesFramework.dll").ToString();
            dtStatisticsRow["TypeName"] = ("TimeSeriesFramework.Statistics.PerformanceStatistics").ToString();
            dtStatisticsRow["MethodName"] = ("GetSystemStatistic_CPUUsage").ToString();

            dtStatistics.Rows.Add(dtStatisticsRow);
            #endregion

            #region [ Measurement Data ]
            /**********************************************************************/
            // Populate measurement table
            dtMeasurement = new DataTable("ActiveMeasurements");
            dtMeasurement.Columns.Add("ID");
            dtMeasurement.Columns.Add("SignalID");
            dtMeasurement.Columns.Add("PointTag");
            dtMeasurement.Columns.Add("Adder");
            dtMeasurement.Columns.Add("Multiplier");

            DataRow dtMeasurementRow = dtMeasurement.NewRow();

            dtMeasurementRow["ID"] = (10).ToString();
            dtMeasurementRow["SignalID"] = Guid.NewGuid().ToString();
            dtMeasurementRow["PointTag"] = "GPA_PMU_34:21";
            dtMeasurementRow["Adder"] = "UIUC\\dkholine";
            dtMeasurementRow["Multiplier"] = "0.033";

            dtMeasurement.Rows.Add(dtMeasurementRow);
            #endregion

            // Measurement from IMeasurement
            #region [ Measurement ]
            // Init Measurements Case
            StubIMeasurementValue measurement = new StubIMeasurementValue();

            m_Measurement = new Measurement(streamID, timestamp, flags, measurement);
            #endregion

            // Fill in Adapters data
            #region [ Input Adapter Data ]
            // Create
            dtInputAdapter = new DataTable("InputAdapter");
            // Add adapter description
            dtInputAdapter.Columns.Add("ID");
            dtInputAdapter.Columns.Add("AdapterName");
            dtInputAdapter.Columns.Add("AssemblyName");
            dtInputAdapter.Columns.Add("TypeName");
            dtInputAdapter.Columns.Add("ConnectinString");
            dtInputAdapter.Columns.Add("incomingStreamIDs");
            dtInputAdapter.Columns.Add("outgoingStreamIDs");

            DataRow dtInputAdapterRow = dtInputAdapter.NewRow();

            dtInputAdapterRow["ID"] = Guid.NewGuid().ToString();
            dtInputAdapterRow["AdapterName"] = "ActionAdapter";
            dtInputAdapterRow["AssemblyName"] = "PhasorProtocols.dll";
            dtInputAdapterRow["TypeName"] = "PhasorProtocols.IeeeC37_118.Concentrator";
            dtInputAdapterRow["ConnectinString"] = "requireAuthentication=false; allowSynchronizedSubscription=false; useBaseTimeOffsets=true";
            dtInputAdapterRow["incomingStreamIDs"] = Guid.NewGuid().ToString();
            dtInputAdapterRow["outgoingStreamIDs"] = Guid.NewGuid().ToString();


            dtInputAdapter.Rows.Add(dtInputAdapterRow);
            #endregion

            #region [ Action Adapter Data ]

            dtActionAdapter = new DataTable("ActionAdapter");
            //Add adapter description
            dtActionAdapter.Columns.Add("ID");
            dtActionAdapter.Columns.Add("AdapterName");
            dtActionAdapter.Columns.Add("incomingStreamIDs");
            dtActionAdapter.Columns.Add("outgoingStreamIDs");

            DataRow dtActionAdapterRow = dtActionAdapter.NewRow();

            dtActionAdapterRow["ID"] = Guid.NewGuid().ToString();
            dtActionAdapterRow["AdapterName"] = "ActionAdapter";
            dtActionAdapterRow["incomingStreamIDs"] = Guid.NewGuid().ToString() ;
            dtActionAdapterRow["outgoingStreamIDs"] = Guid.NewGuid().ToString() ;

            dtActionAdapter.Rows.Add(dtActionAdapterRow);
            #endregion

            #region [ Output Adapter Data ]
            // Create  
            dtOutputAdapter = new DataTable("OutputAdapter");
            dtOutputAdapter.Columns.Add("ID");
            dtOutputAdapter.Columns.Add("Acronym");
            dtOutputAdapter.Columns.Add("LoadOrder");
            dtOutputAdapter.Columns.Add("ParentID");

            // Add row
            DataRow dtOutputAdapterRow = dtOutputAdapter.NewRow();

            dtOutputAdapterRow["ID"] = ("1").ToString();
            dtOutputAdapterRow["Acronym"] = ("UnitTesting").ToString();
            dtOutputAdapterRow["LoadOrder"] = ("1").ToString();
            dtOutputAdapterRow["ParentID"] = ("1").ToString();

            // Populate
            dtOutputAdapter.Rows.Add(dtOutputAdapterRow);

            #endregion

            // Initialize adapters
            #region [ Input Adapter Initialize ]
            dataSetInput.Tables.Add(dtInputAdapter);
            dataSetInput.Tables.Add(dtMeasurement.Clone());
            dataSetInput.Tables.Add(dtStatistics.Clone());
            #endregion

            #region [ Action Adapter Initialize ]
            dataSetAction.Tables.Add(dtActionAdapter);
            dataSetAction.Tables.Add(dtMeasurement.Clone());
            dataSetAction.Tables.Add(dtStatistics.Clone());
            #endregion

            #region [ Output Adapter Initialize ]
            dataSetOutput.Tables.Add(dtOutputAdapter);
            dataSetOutput.Tables.Add(dtMeasurement.Clone());
            dataSetOutput.Tables.Add(dtStatistics.Clone());
            #endregion

            #region [ Populate ]
            
            //conf_input.ExtractConfiguration(dataSetInput);
            //conf_action.ExtractConfiguration(dataSetAction);
            //conf_output.ExtractConfiguration(dataSetOutput);

            // Declare Adapters
            m_InputAdapter = new MockInputAdapter(conf_input);
            m_ActionAdapter = new MockActionAdapter(conf_action);
            m_OutputAdapter = new MockOutputAdapter(conf_output);
            #endregion
        }
        #endregion

        #region [ Methods ]
        #endregion
    }
}
